<?php

namespace app\index\controller;

use Predis\Client;
use think\Controller;
use think\Cookie;
use think\Session;

class Base extends Controller
{
	public $list;

    public function _initialize()
    {
        // 判断cookie
        if (!empty(cookie('index'))) {
            $index = cookie('index');
            $this->list = $this->cookieVali($index);
        } else {
            // 判断session
            if (!empty(session('index'))) {
                $index = session('index');
                $this->list = $this->vali($index);
            }
        }
    }

    // cookie验证
    public function cookieVali($index)
    {
        $redis = new Client;
        $userTel = $index['tel'];
        $id = $redis->get('index:name:' . $userTel . ':id');
        // 判断redis中数据
        if ($id) {
           $res = $this->redis($index, $id);
            return $res;
        } else {
            // 判断数据库数据
            $pwd = $index['pwd'];
            $model = Model('login');
            $res = $model->mysql($userTel, $pwd);
            if ($res) {
                $res = $this->mysql($index, $res);
                return $res;
            }
        }
    }

    // 无cookie验证
    public function vali($index)
    {
        $userTel = session('index')['tel'];
        $redis = new Client;
        $id = $redis->get('index:name:' . $userTel . ':id');
        // 判断redis中数据
        if ($id) {
            $res = $this->redis($index,$id);
            return $res;
        } else {
            // 判断数据库数据
            $pwd = $index['pwd'];
            $model = Model('login');
            $res = $model->mysql($userTel, $pwd);
            if ($res) {
                $res = $this->mysql($index, $res);
                return $res;
            }
        }
    }

    // 有redis验证
    public function redis($index,$id)
    {
        $redis = new Client;
        $pwd = $redis->hget('index:' . $id, 'pwd');
        if ($pwd == $index['pwd']) {
            $list = [
                'name' => $redis->hget('index:' . $id, 'name'),
                'id' => $redis->hget('index:'.$id, 'id'),
            ];
            return $list;
        }
    }

    // mysql验证
    public function mysql($index, $res)
    {
        $redis = new Client;
        $ip = Request::instance()->ip();
        $list = [
            'login_ip' => $ip,
            'last_login_time' => time(),
            'times' => ++$res['times'],
        ];

        $dotime = model('Login');
        $dotime->uplogin($list, $res['id']);

        // 设置自增id
        $id = $redis->incr('index:id');

        // 将用户ID存入redis
        $redis->setex('index:name:' . $index['tel'] . ':id', '7200', $id);
        // 将用户信息以自增 id 存入redis(hash)
        $redis->hset('index:' . $id, 'pwd', $index['pwd']);
        $redis->hset('index:' . $id, 'name', $res['nickname']);
        $redis->hset('index:' . $id, 'id', $res['id']);
        $redis->hset('index:' . $id, 'times', ++$res['times']);

        $list = [
            'name' => $res['nickname'],
            'id' => $res['id'],
        ];
        return $list;
    }
}
